home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / render2pixmap / offscreen_render.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  6KB  |  225 lines

  1. /*
  2.  * Copyright (c) 1993-94, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /*
  23.  * 1993 Simon Hui -- Silicon Graphics Computer Systems
  24.  *
  25.  * Test drawing to pixmaps of various sizes.
  26.  *
  27.  * Modified to be part of an opengl render to file test case
  28.  * by Ann LaGrone -- Silicon Graphics Computer Systems, 1994.
  29.  *
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <GL/gl.h>
  36. #include <GL/glx.h>
  37. #include <GL/glu.h>
  38. #include <X11/keysym.h>
  39. #include "util.h"
  40.  
  41. /*
  42.  * Size of pixmap to create,
  43.  */
  44.  
  45. int size = 300;  /* pixmap size, this example is square */
  46.  
  47. #define GAP 10  /* border */
  48.  
  49. long width, height;
  50. XVisualInfo *vi;
  51. GC xgc;
  52. Display *dpy;
  53. Window window;
  54. Pixmap pix;
  55. GLXPixmap glxpix;
  56. GLXContext cx;
  57.  
  58. /* Scene drawing routine */
  59. static void drawScene(void)
  60. {
  61.  
  62.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  63.     glEnable(GL_BLEND);
  64.     glDisable(GL_DEPTH_TEST);
  65.  
  66.     /*
  67.      * Draw the right triangle.
  68.      */
  69.     glColor4f(0., 1., 0., 1.);
  70.     glBegin(GL_TRIANGLES);
  71.     glVertex3f( 0.9, -0.9, -30.0);
  72.     glVertex3f( 0.9,  0.9, -30.0);
  73.     glVertex3f(-0.9,  0.0, -30.0);
  74.     glEnd();
  75.  
  76.     /*
  77.      * Draw the left triangle.
  78.      */
  79.     glColor4f(0., 0. ,1., .2);
  80.     glBegin(GL_TRIANGLES);
  81.     glVertex3f(-0.9, -0.9, -40.0);
  82.     glVertex3f(-0.9,  0.9, -40.0);
  83.     glVertex3f( 0.9,  0.0, -25.0);
  84.     glEnd();
  85. }
  86.  
  87. /* 
  88.    Switch contexts to offscreen rendering and output the
  89.    image to a pixmap, then call writeimage to dump it to
  90.    a file in sgi image file format. 
  91. */
  92. static void tofile(void)
  93. {
  94.     int i=0, x=GAP, y=GAP;
  95.     XImage *ximage;
  96.     XWindowAttributes win_info;
  97.  
  98.  
  99.     XClearWindow(dpy, window);
  100.     glXMakeCurrent(dpy, glxpix, cx);
  101.     glViewport(0, 0, size, size);
  102.     glMatrixMode(GL_PROJECTION);
  103.     glLoadIdentity();
  104.     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  105.     glMatrixMode(GL_MODELVIEW);
  106.     glEnable(GL_DEPTH_TEST);
  107.     glClearColor(1, 1, 1, 1);
  108.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  109.     
  110.     drawScene();
  111.     
  112.     glXWaitGL();
  113.     ximage = XGetImage(dpy, pix, 0, 0, size, size, 0xffffff,
  114.                        ZPixmap);
  115.     /* Set up mask fields for writeimage program */
  116.  
  117.     if(!XGetWindowAttributes(dpy, window, &win_info)) {
  118.         printf("Can't get target window attributes.");
  119.     exit(1);
  120.     }
  121.  
  122.     ximage->red_mask = win_info.visual->red_mask;
  123.     ximage->green_mask = win_info.visual->green_mask;
  124.     ximage->blue_mask = win_info.visual->blue_mask;
  125.  
  126.     /* write out image in SGI image file format */
  127.     writeimage(ximage, "offscreen.rgb");
  128.  
  129.     /* Copy the pixmap image to the screen */
  130.     XCopyArea(dpy, pix, window, xgc, 0, 0,
  131.               size, size, x, y);
  132.     glFlush();
  133.     XFlush(dpy);
  134. }
  135.  
  136.  
  137. /* Standard redraw routine to the screen's drawable */
  138. static void redraw(void)
  139. {
  140.     int i=0, x=GAP, y=GAP;
  141.     XImage *ximage;
  142.  
  143.     XClearWindow(dpy, window);
  144.     glXMakeCurrent(dpy, window, cx);
  145.     glViewport(0, 0, size, size);
  146.     glMatrixMode(GL_PROJECTION);
  147.     glLoadIdentity();
  148.     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  149.     glMatrixMode(GL_MODELVIEW);
  150.     glEnable(GL_DEPTH_TEST);
  151.     glClearColor(1, 1, 1, 1);
  152.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  153.     
  154.     drawScene();
  155.     
  156.     glFlush();
  157.     XFlush(dpy);
  158. }
  159.  
  160. static int attributes[] = {
  161.     GLX_RGBA,
  162.     GLX_RED_SIZE, 1,
  163.     GLX_GREEN_SIZE, 1,
  164.     GLX_BLUE_SIZE, 1,
  165.     GLX_DEPTH_SIZE, 1,
  166.     None,
  167. };
  168.  
  169. /* input events -
  170.     esc            quit/exit
  171.     left arrow    redraw
  172.     right arrow    off screen render to file
  173. */
  174. void keypress( KeySym ks, Bool *exit, Bool *redraw, Bool *tofile )
  175. {
  176.     switch (ks) {
  177.       case XK_Left:
  178.     *redraw = True;
  179.     break;
  180.       case XK_Right:
  181.     *tofile = True;
  182.         break;
  183.       case XK_Escape:
  184.     *exit = True;
  185.     break;
  186.     }
  187. }
  188.  
  189. int main(int argc, char **argv)
  190. {
  191.     XEvent event;
  192.     GLboolean needDisplay;
  193.     int i, total, maxwidth;
  194.  
  195.     /*
  196.      * Make the window big enough to hold all the pixmaps.
  197.      */
  198.     total = GAP;
  199.     maxwidth = 0;
  200.     total += size + GAP;
  201.     if (size > maxwidth) {
  202.         maxwidth = size;
  203.     }
  204.     width = total;
  205.     height = maxwidth + 2*GAP;
  206.  
  207.     window = utilCreateWindow( width, height, 50, 50, None, attributes, "Gray",
  208.                    argc, argv, NULL, &dpy, &vi, NULL, &xgc); 
  209.     
  210.     cx = glXCreateContext(dpy, vi, 0, False);
  211.     if (!glXMakeCurrent(dpy, window, cx)) {
  212.     fprintf(stderr, "Can't make window current to context\n");
  213.     return -1;
  214.     }
  215.     pix = XCreatePixmap(dpy, window, size, size, vi->depth);
  216.     glxpix = glXCreateGLXPixmap(dpy, vi, pix);
  217.  
  218.     redraw();
  219.     utilEventLoop(dpy, NULL, keypress, redraw, tofile);
  220.  
  221.     glXMakeCurrent(dpy, None, NULL);
  222.     glXDestroyContext(dpy, cx);
  223.     XCloseDisplay(dpy);
  224. }
  225.